package logic

import (
	"context"
	"errors"
	"fresh-box-manage/models/mysql"

	"fresh-box-manage/freshrpc/freshrpc"
	"fresh-box-manage/freshrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type FreshDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFreshDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FreshDelLogic {
	return &FreshDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 删除
func (l *FreshDelLogic) FreshDel(in *freshrpc.FreshDelRequest) (*freshrpc.FreshDelResponse, error) {
	// todo: add your logic here and delete this line

	var fresh mysql.Fresh

	if err := fresh.FreshFindById(mysql.DB, in.FreshId); err != nil {
		return nil, errors.New("该商品不存在,不允许删除")
	}

	if err := fresh.FreshDel(mysql.DB, in.FreshId); err != nil {
		return nil, err
	}

	return &freshrpc.FreshDelResponse{}, nil
}
